<template>
  <div>
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="130px">
      <el-form-item label="pid:" prop="mchId">
        <el-input v-model.trim="temp.mchId" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="应用appId:" prop="appId">
        <el-input v-model.trim="temp.appId" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="支付宝公钥:" prop="accKey">
        <el-input v-model.trim="temp.accKey" type="textarea" :rows="3" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="应用私钥:" prop="accKey2">
        <el-input v-model.trim="temp.accKey2" type="textarea" :rows="3" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="$loadingPlugin.submit" @click="save">保存配置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { queryWxPayDefaultData, saveWxPayInfo } from '@/api'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ZFBPayTab',
    data() {
      return {
        appIdList: [],
        defaultData: {},
        temp: {
          mchId: '',
          appId: '',
          accKey: '',
          accKey2: ''
        },
        rules: {
          appId: [{ required: true, message: '请输入应用appId', trigger: 'change' }],
          accKey: [{ required: true, message: '请输入支付宝公钥', trigger: 'change' }],
          accKey2: [{ required: true, message: '请输入应用私钥', trigger: 'change' }]
        }
      }
    },
    computed: {
      ...mapGetters([
        'userInfo'
      ])
    },
    async created() {
      await Promise.all([this.getDefaultData()])
    },
    methods: {
      async getDefaultData() {
        const res = await queryWxPayDefaultData({ accountType: 2 })
        this.temp = Object.assign(this.temp, res.data)
        if (res.data.accKey) {
          this.temp.accKey = '●●●●●●●●●●●●●●●●●●●●●'
        }
        if (res.data.accKey2) {
          this.temp.accKey2 = '●●●●●●●●●●●●●●●●●●●●●'
        }
        this.defaultData = res.data
      },
      save() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            appId: this.temp.appId,
            mchId: this.temp.mchId,
            accountType: 2
          }
          if (this.temp.accKey.indexOf('●●●●●●●●●●●●●●●●●●●●●') === -1) {
            params.accKey = this.temp.accKey
          }
          if (this.temp.accKey2.indexOf('●●●●●●●●●●●●●●●●●●●●●') === -1) {
            params.accKey2 = this.temp.accKey2
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await saveWxPayInfo(params)
        this.$notify({
          title: '提示',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
      }
    }
  }
</script>

<style scoped>

</style>
